# coding: utf-8
#
import time
import traceback

import uiautomator2 as u2


def share(key, lang_tpye):
    # key = '3种你需要尽快抛弃的过度炒作的加密货币'
    try:
        print(key)
        print(lang_tpye)
        # pc
        # d = u2.connect()
        # 手机内连接
        d = u2.connect('0.0.0.0')
        app_package_wx = "com.tencent.mm"

        d.implicitly_wait(120.0)  # 设置元素查找等待时间
        d.unlock()
        time.sleep(1)
        d.app_stop("org.cryptosquare.app")
        d.app_start("org.cryptosquare.app")
        search_key = key
        time.sleep(2)
        if lang_tpye == 1 and not d(description="搜索").exists():
            d(description="\nDrawer").click()
            d(description="Choose Language\nTab 6 of 6").click()
            d(description="Chinese").click()
            d(description="Confirm").click()
        elif lang_tpye == 0 and not d(description="Search").exists():
            d(description="\n抽屉").click()
            d(description="选择语言\n第 6 个标签，共 6 个").click()
            d(description="English").click()
            d(description="确认").click()
        time.sleep(2)
        if d(description="搜索").exists():
            # 分享中文
            d(description="搜索").click()
            time.sleep(1)
            d.send_keys(search_key, clear=True)
            d.press('enter')
            time.sleep(10)
            d.click(0.095, 0.439)
            print(d(description=search_key).exists())
            d(description="\naction").click()
            time.sleep(3)
            d(description="分享").click()
            time.sleep(3)
            d.xpath(
                '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.Button[3]').click()
            time.sleep(3)
            d.xpath('//*[@resource-id="com.tencent.mm:id/og6"]/android.widget.LinearLayout[1]').click()
            time.sleep(10)
            d.send_keys("张东洋", clear=True)
            time.sleep(3)
            d(resourceId="com.tencent.mm:id/odf", text="张东洋").click()
            time.sleep(3)
            d(resourceId="com.tencent.mm:id/jln").click()
            time.sleep(3)
            # 休息1秒
            d(resourceId="com.tencent.mm:id/jln").click()
        else:
            # 分享英文
            d(description="Search").click()
            time.sleep(1)
            d.send_keys(search_key, clear=True)
            d.press('enter')
            time.sleep(10)
            d.click(0.095, 0.439)
            print(d(description=search_key).exists())
            d(description="\naction").click()
            time.sleep(3)
            d(description="Share").click()
            time.sleep(3)
            d.xpath(
                '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.Button[3]').click()
            time.sleep(3)
            d.xpath(
                '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.Button[3]').click()
            time.sleep(3)
            d.xpath('//*[@resource-id="com.tencent.mm:id/og6"]/android.widget.LinearLayout[1]').click()
            time.sleep(10)
            d.send_keys("张东洋", clear=True)
            time.sleep(3)
            d(resourceId="com.tencent.mm:id/odf", text="张东洋").click()
            time.sleep(3)
            d(resourceId="com.tencent.mm:id/jln").click()
            time.sleep(3)
            # 休息1秒
            d(resourceId="com.tencent.mm:id/jln").click()
        # time.sleep(2)
        # d.app_stop(app_package_wx)
        # time.sleep(1)
        # d.app_start(app_package_wx)
        # time.sleep(10)
        # d(resourceId="com.tencent.mm:id/f2s", text="微信").click()
        time.sleep(5)
        d(resourceId="com.tencent.mm:id/jha").click()
        time.sleep(3)
        d.send_keys("张东洋", clear=True)
        time.sleep(3)
        d(resourceId="com.tencent.mm:id/odf", text="张东洋").click()
        # d(resourceId="com.tencent.mm:id/kbq", text="张东洋").click()
        time.sleep(2)
        share_to_list = [
            # ["张东洋"],
            ["cs web3 门户",
             "Bitpush（比推）web3交流群",
             '华尔街币圈',
             '比推Bitpush朋友群💐🐶🍻',
             '区块链与华尔街',
             '区块链技术讨论社区',
             '比推Bitpush区块链讨论群🌊',
             '比推Bitpush加密讨论群2🌊',
             '比推Bitpush加密讨论群3🌊'
             ], [
                'Bitpush + Tiger Crypto 加密投资',
                '老友记 #1',
                '东河论剑-加密货币4⃣️',
                '斯坦福区块链科学大会2022',
                # 'FilSwan开发者群',被移除
                'BBS.NYC',
                '金融科技媒体群Pro',
                'ABGA Friends Community',
                '华人矿业友朋',
            ],
            # [
            #     '仲爷Infinite888'被移除
            # ]
        ]

        # 直接定位当前屏幕的元素
        # d(resourceId="com.tencent.mm:id/b3o", text="美国总统参选人小罗伯特·肯尼迪：给我的7个孩子每人购买了2个比特币！").long_click()

        # 直接当前屏幕定位
        # d(resourceId="com.tencent.mm:id/kpm", text="cs web3 门户").click()
        # d(resourceId="com.tencent.mm:id/kpm", text="张东洋").click()
        # d(resourceId="com.tencent.mm:id/kpm", text="新加坡元宇宙Web3峰会").click()

        # 滑动定位
        for item in share_to_list:
            # 滑动查找元素
            # d(className="androidx.recyclerview.widget.RecyclerView", resourceId="com.tencent.mm:id/b79") \
            #     .child_by_text(key,
            #                    className="android.widget.TextView") \
            #     .long_click()
            time.sleep(1)
            d(resourceId="com.tencent.mm:id/bjx", text=search_key).long_click()
            time.sleep(1)
            d(resourceId="com.tencent.mm:id/obc", text="转发").click()
            time.sleep(1)
            d(resourceId="com.tencent.mm:id/fp").click()
            time.sleep(1)
            d(resourceId="com.tencent.mm:id/k13").click()
            for child in item:
                print('分享：' + child)
                time.sleep(1)
                d.send_keys(child, clear=False)
                time.sleep(1)
                try:
                    d(resourceId="com.tencent.mm:id/odf", text=child).click()
                except Exception as e:
                    print(e)
                    d.clear_text()
                    pass
                # time.sleep(1)
                # d(className="android.widget.ListView", resourceId="com.tencent.mm:id/j9m") \
                #     .child_by_text(child, className="android.widget.TextView") \
                #     .click()
            time.sleep(1)
            # 发送
            d(resourceId="com.tencent.mm:id/fp").click()
            # 弹窗
            # 确认
            print(len(item))
            time.sleep(1)
            d(resourceId="com.tencent.mm:id/jln").click()
            # 取消
            # d(resourceId="com.tencent.mm:id/gui").click()
            time.sleep(2)
        # 左上角返回
        d.xpath('//*[@resource-id="com.tencent.mm:id/cob"]/android.widget.LinearLayout[1]').click()
        time.sleep(1)
        d.press("back")
        time.sleep(1)
        d.app_start("com.termux")
        time.sleep(1)
    except:
        exc = traceback.format_exc()
        print(exc)
        raise Exception(exc)
        pass
    # d.screen_off()


if __name__ == '__main__':
    share('火爆的跨年夜生意：民宿日入2万，气球一晚卖8千', 1)
